<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: Operator overloads</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_script_class_ops.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Operator overloads </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>It is possible to define what should be done when an operator is used with a script class. While not necessary in most scripts it can be useful to improve readability of the code.</p>
<p>This is called operator overloading, and is done by implementing specific class methods. The compiler will recognize and use these methods when it compiles expressions involving the overloaded operators and the script class.</p>
<h2><a class="anchor" id="doc_script_class_unary_ops"></a>
Prefixed unary operators</h2>
<table  cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="80"><b>op</b></td><td width="120"><b>opfunc</b> </td></tr>
<tr>
<td>- </td><td>opNeg </td></tr>
<tr>
<td>~ </td><td>opCom </td></tr>
<tr>
<td>++ </td><td>opPreInc </td></tr>
<tr>
<td>-- </td><td>opPreDec </td></tr>
</table>
<p>When the expression <code><em>op</em> a</code> is compiled, the compiler will rewrite it as <code>a.<em>opfunc</em>()</code> and compile that instead.</p>
<h2><a class="anchor" id="doc_script_class_unary2_ops"></a>
Postfixed unary operators</h2>
<table  cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="80"><b>op</b></td><td width="120"><b>opfunc</b> </td></tr>
<tr>
<td>++ </td><td>opPostInc </td></tr>
<tr>
<td>-- </td><td>opPostDec </td></tr>
</table>
<p>When the expression <code>a <em>op</em></code> is compiled, the compiler will rewrite it as <code>a.<em>opfunc</em>()</code> and compile that instead.</p>
<h2><a class="anchor" id="doc_script_class_cmp_ops"></a>
Comparison operators</h2>
<table  cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="80"><b>op</b></td><td width="120"><b>opfunc</b> </td></tr>
<tr>
<td>== </td><td>opEquals </td></tr>
<tr>
<td>!= </td><td>opEquals </td></tr>
<tr>
<td>&lt; </td><td>opCmp  </td></tr>
<tr>
<td>&lt;= </td><td>opCmp  </td></tr>
<tr>
<td>&gt; </td><td>opCmp  </td></tr>
<tr>
<td>&gt;= </td><td>opCmp  </td></tr>
</table>
<p>The <code>a == b</code> expression will be rewritten as <code>a.opEquals(b)</code> and <code>b.opEquals(a)</code> and then the best match will be used. <code>!=</code> is treated similarly, except that the result is negated. The opEquals method must be implemented to return a <code>bool</code> in order to be considered by the compiler.</p>
<p>The comparison operators are rewritten as <code>a.opCmp(b) <em>op</em> 0</code> and <code>0 <em>op</em> b.opCmp(a)</code> and then the best match is used. The opCmp method must be implemented to return a <code>int</code> in order to be considered by the compiler. If the method argument is to be considered larger than the object then the method should return a negative value. If they are supposed to be equal the return value should be 0.</p>
<p>If an equality check is made and the opEquals method is not available the compiler looks for the opCmp method instead. So if the opCmp method is available it is really not necesary to implement the opEquals method, except for optimization reasons.</p>
<h2><a class="anchor" id="doc_script_class_assign_ops"></a>
Assignment operators</h2>
<table  cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="80"><b>op</b></td><td width="120"><b>opfunc</b> </td></tr>
<tr>
<td>= </td><td>opAssign  </td></tr>
<tr>
<td>+= </td><td>opAddAssign  </td></tr>
<tr>
<td>-= </td><td>opSubAssign  </td></tr>
<tr>
<td>*= </td><td>opMulAssign  </td></tr>
<tr>
<td>/= </td><td>opDivAssign  </td></tr>
<tr>
<td>%= </td><td>opModAssign  </td></tr>
<tr>
<td>&amp;= </td><td>opAndAssign  </td></tr>
<tr>
<td>|= </td><td>opOrAssign  </td></tr>
<tr>
<td>^= </td><td>opXorAssign  </td></tr>
<tr>
<td>&lt;&lt;= </td><td>opShlAssign  </td></tr>
<tr>
<td>&gt;&gt;= </td><td>opShrAssign  </td></tr>
<tr>
<td>&gt;&gt;&gt;= </td><td>opUShrAssign </td></tr>
</table>
<p>The assignment expressions <code>a <em>op</em> b</code> are rewritten as <code>a.<em>opfunc</em>(b)</code> and then the best matching method is used. An assignment operator can for example be implemented like this:</p>
<pre>
  obj@ opAssign(const obj &amp;in other)
  {
    // Do the proper assignment
    ...</pre><pre>    // Return a handle to self, so that multiple assignments can be chained
    return this;
  }
</pre><p>All script classes have a default assignment operator that does a bitwise copy of the content of the class, so if that is all you want to do, then there is no need to implement this method.</p>
<h2><a class="anchor" id="doc_script_class_binary_ops"></a>
Binary operators</h2>
<table  cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="80"><b>op</b></td><td width="120"><b>opfunc</b></td><td><b>opfunc_r</b> </td></tr>
<tr>
<td>+ </td><td>opAdd </td><td>opAdd_r </td></tr>
<tr>
<td>- </td><td>opSub </td><td>opSub_r </td></tr>
<tr>
<td>* </td><td>opMul </td><td>opMul_r </td></tr>
<tr>
<td>/ </td><td>opDiv </td><td>opDiv_r </td></tr>
<tr>
<td>% </td><td>opMod </td><td>opMod_r </td></tr>
<tr>
<td>&amp; </td><td>opAnd </td><td>opAnd_r </td></tr>
<tr>
<td>| </td><td>opOr </td><td>opOr_r </td></tr>
<tr>
<td>^ </td><td>opXor </td><td>opXor_r </td></tr>
<tr>
<td>&lt;&lt; </td><td>opShl </td><td>opShl_r </td></tr>
<tr>
<td>&gt;&gt; </td><td>opShr </td><td>opShr_r </td></tr>
<tr>
<td>&gt;&gt;&gt; </td><td>opUShr </td><td>opUShr_r </td></tr>
</table>
<p>The expressions with binary operators <code>a <em>op</em> b</code> will be rewritten as <code>a.<em>opfunc</em>(b)</code> and <code>b.<em>opfunc_r</em>(a)</code> and then the best match will be used.</p>
<h2><a class="anchor" id="doc_script_class_index_op"></a>
Index operators</h2>
<table  cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="80"><b>op</b></td><td width="120"><b>opfunc</b> </td></tr>
<tr>
<td>[] </td><td>opIndex </td></tr>
</table>
<p>When the expression <code>a[i]</code> is compiled, the compiler will rewrite it as <code>a.opIndex(i)</code> and compile that instead.</p>
<p>The index operator can also be formed similarly to <a class="el" href="doc_script_class_prop.html">property accessors</a>. The get accessor should then be named <code>get_opIndex</code> and have one parameter for the indexing. The set accessor should be named <code>set_opIndex</code> and have two parameters, the first is for the indexing, and the second for the new value.</p>
<pre>
  class MyObj
  {
    float get_opIndex(int idx) const       { return 0; }
    void set_opIndex(int idx, float value) { }
  }
</pre><p>When the expression <code>a[i]</code> is used to retrieve the value, the compiler will rewrite it as <code>a.get_opIndex(i)</code>. When the expression is used to set the value, the compiler will rewrite it as <code>a.set_opIndex(i, expr)</code>. </p>
</div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:04 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
